import 'package:flutter/material.dart';

void main() => runApp(MyApp());

class MyApp extends StatelessWidget {
  @override
  Widget build(BuildContext context) {
    return MaterialApp(home: PageViewWithController());
  }
}

/// PageView using PageController to control navigation between pages programmatically.
class PageViewWithController extends StatefulWidget {
  @override
  _PageViewWithControllerState createState() => _PageViewWithControllerState();
}

class _PageViewWithControllerState extends State<PageViewWithController> {
  final PageController _pageController = PageController(
    initialPage: 1,
    viewportFraction: 0.5,
  );

  @override
  void dispose() {
    _pageController.dispose();
    super.dispose();
  }

  @override
  Widget build(BuildContext context) {
    return Scaffold(
      appBar: AppBar(title: Text("PageView with Controller")),
      body: PageView(
        controller: _pageController,
        children: [
          Container(color: Colors.orange, child: Center(child: Text("Page 1"))),
          Container(color: Colors.purple, child: Center(child: Text("Page 2"))),
          Container(color: Colors.cyan, child: Center(child: Text("Page 3"))),
        ],
      ),
      floatingActionButton: FloatingActionButton(
        onPressed: () {
          _pageController.nextPage(
            duration: Duration(milliseconds: 500),
            curve: Curves.easeInOut,
          );
        },
        child: Icon(Icons.arrow_forward),
      ),
    );
  }
}
